﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2012 
 * 
 * 文件名：ArticleController.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：zhangqiang@bamabest.com
 * 日 期：2013-03-04 01:41:43
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using IManager;
using Webdiyer.WebControls.Mvc;
using WebPlatform.FiltersAndSession;

namespace WebPlatform.Areas.CPP.Controllers
{

    /// <summary>
    /// 
    /// </summary>
    public class CArticleController : Controller
    {

        public IManagerArticle IMArticle { get; set; }


        public IManagerContent IMContent { get; set; }


        public IManagerCompany IMCompany { get; set; }

        /// <summary>
        /// 登录会话操作
        /// </summary>
        public Sessions Sessions { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ActionResult Index(string current, string filters, string aid, string num, int page = 1, int pageSize = 15)
        {
            var partition = IMArticle.GetPartition(aid);

            if (partition != null)
            {
                ViewBag.Title = partition.Name;
            }
            else
            {
                throw new HttpException(404, string.Format("页面不存在", Request.Path));
            }
            var p = PredicateExtension.True<ArticleInfo>();
            p = p.And(u => u.CompanyID == 1 && u.PartitionId == aid);
            if (!string.IsNullOrEmpty(filters))
            {
                p = p.And(u => u.Title.Contains(filters) || u.ArticleDetail.Content.Contains(filters));
            }
            if (!string.IsNullOrEmpty(num))
            {
                p = p.And(u => u.Numbers == num);
            }
            var count = IMContent.LoadCount(p);
            var list = IMContent.LoadAll(p, page, pageSize, new OrderByExpression<ArticleInfo, DateTime>(u => u.ReleaseDate, true));

            PagedList<ArticleInfo> plist =
                new PagedList<ArticleInfo>(list, page, pageSize, count);

            ViewBag.HSDT = current;
            return View(plist);
        }


        /// <summary>
        /// 
        /// </summary>
        public ActionResult Filters(string filters, int page = 1, int pageSize = 15)
        {

            var p = PredicateExtension.True<ArticleInfo>();
            p = p.And(u => u.CompanyID == 1 && u.PartitionId.StartsWith("HSZZ") && u.PartitionId.Substring(u.PartitionId.Length - 3, 3) != "PIC");
            if (!string.IsNullOrEmpty(filters))
            {
                p = p.And(u => u.Title.Contains(filters) || u.ArticleDetail.Content.Contains(filters));
            }
            var count = IMContent.LoadCount(p);
            var list = IMContent.LoadAll(p, page, pageSize, new OrderByExpression<ArticleInfo, DateTime>(u => u.ReleaseDate, true));

            PagedList<ArticleInfo> plist =
                new PagedList<ArticleInfo>(list, page, pageSize, count);

            return View(plist);
        }



        public ActionResult Detail(string current, int aid = 0)
        {
            var article = IMContent.GetArticle(aid);
            if (article == null)
            {
                throw new HttpException(404, string.Format("页面不存在", Request.Path));
            }
            ViewBag.HSDT = current;
            return View(article);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Gallery(int pageIndex = 1)
        {
            var where = PredicateExtension.True<ArticleInfo>();
            where = where.And(u => u.CompanyID == 1);
            where = where.And(u => u.PartitionId == "HSZZXWPIC");
            var count = IMContent.LoadCount(where);
            int pageSize = 8;
            int pages = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageIndex > pages)
            {
                pageIndex = pages;
            }
            var list = IMContent.LoadAll(where, pageIndex, pageSize, new OrderByExpression<ArticleInfo, DateTime>(u => u.ReleaseDate, true));
            ViewBag.PageIndex = pageIndex;
            ViewBag.PageCount = pages;
            return View(list);
        }



        public ActionResult Catalog(string num, int aid = 0)
        {
            var article = IMContent.GetArticle(aid);
            if (article == null)
            {
                throw new HttpException(404, string.Format("页面不存在", Request.Path));
            }
            ViewBag.Num = num;
            return View(article);
        }


        public ActionResult Catalogs(string aid)
        {
            var partition = IMArticle.GetPartition(aid);

            if (partition != null)
            {
                ViewBag.Partition = partition;
            }
            else
            {
                throw new HttpException(404, string.Format("页面不存在", Request.Path));
            }

            var p = PredicateExtension.True<ArticleInfo>();
            p = p.And(u => u.CompanyID == 1 && u.PartitionId == "HSZZFMPIC");
           
            var count = IMContent.LoadCount(p);
            var list = IMContent.LoadAll(p, 1, count, new OrderByExpression<ArticleInfo, DateTime>(u => u.ReleaseDate, true));

            return View(list);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public ActionResult ArticlePartial(string pid, string num)
        {
            var p = PredicateExtension.True<ArticleInfo>();
            p = p.And(u => u.CompanyID == 1 && u.PartitionId == pid && u.Numbers == num);

            var count = IMContent.LoadCount(p);
            var list = IMContent.LoadAll(p, 1, count, new OrderByExpression<ArticleInfo, DateTime>(u => u.ReleaseDate, true));

            return PartialView(list);
        }


        public ActionResult Contact()
        {
            var company = IMCompany.GetCompany(1);
            return View(company);
        }

    }
}
